#include "stm.h"

int read_temp_hum(unsigned char cmd)
{
    unsigned short data = 0, ret = 0;
    // 封装读消息结构体
    struct i2c_msg r_msg[] = {
        {
            .addr = cli->addr,
            .flags = 0,
            .len = 1,
            .buf = &cmd,
        },
        {
            .addr = cli->addr,
            .flags = 1,
            .len = 2,
            .buf = (u8 *)&data,

        },
    };

    // 发送消息结构体
    if (i2c_transfer(cli->adapter, r_msg, ARRAY_SIZE(r_msg)) != ARRAY_SIZE(r_msg)) {
        printk("__%d__:i2c_transfer is error\n", __LINE__);
        return -1;
    }
    ret |= (data >> 8);
    ret |= (data << 8);
    return ret;
}


long si7006_ioctl(struct file* file, unsigned int cmd, unsigned long args)
{
    int ret = -1;
    if (cmd == GET_SI7006_HUM) {
        ret = read_temp_hum(0xE5);
        if (copy_to_user((void*)args, &ret, _IOC_SIZE(cmd))) {
            printk("copy_to_user is error\n");
            return -1;
        }
    }
    if (cmd == GET_SI7006_TMEP) {
        ret = read_temp_hum(0xE3);
        if (copy_to_user((void*)args, &ret, _IOC_SIZE(cmd))) {
            printk("copy_to_user is error\n");
            return -1;
        }
    }

    return 0;
}